package com.zhongan.tb;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultCellEditor;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.table.TableColumnModel;

public class TBWindow {

    private JFrame frame;
    private JTable table;
    private TBHelper helper = TBHelper.getInstance();
    private TBTableModel tableModel;
    private JButton btnSort;
    private JButton btnFee;
    JTextPane textSpeaker = new JTextPane();
    //��һλ�ݽ���
    private int nextSpeakerId = 1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TBWindow window = new TBWindow();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public TBWindow() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setResizable(false);
        frame.setBounds(100, 100, 718, 482);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JScrollPane scrollPane = new JScrollPane();

        btnSort = new JButton("\u6253\u4E71\u987A\u5E8F");
        //����˳��
        btnSort.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textSpeaker.setText("");
                textSpeaker.setBackground(Color.YELLOW);
                nextSpeakerId = 1;
                List<Integer> selected = tableModel.getSelectedId();
                if (selected.size() > 0)
                    helper.riffle(selected.get(0));
                else
                    helper.riffle(-1);
                tableModel = new TBTableModel(helper);
                table.setModel(tableModel);
                table.updateUI();
            }
        });

        btnFee = new JButton("\u62BD\u53D6\u5927\u5956");
        btnFee.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                if (nextSpeakerId < helper.getTeamgroup().size()) {
                    textSpeaker.setBackground(Color.RED);
                    textSpeaker.setText("�� ��\r\n�� ��");
                    return;
                }

                List<Integer> selected = tableModel.getSelectedId();
                if (selected.size() > 0) {
                    helper.calulateFee(selected);
                    textSpeaker.setBackground(Color.RED);
                    textSpeaker.setText("�� ϲ\r\n�� ��");
                } else
                    return;
                tableModel = new TBTableModel(helper);
                table.setModel(tableModel);
                table.updateUI();
            }
        });


        textSpeaker.setFont(new Font("����", Font.PLAIN, 47));
        textSpeaker.setBackground(Color.YELLOW);

        JButton btnshowName = new JButton("\u5F00\u59CB\u4F60\u7684\u8868\u6F14");
        //��ʾ��һλ�ݽ���
        btnshowName.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textSpeaker.setBackground(Color.YELLOW);
                textSpeaker.setText(helper.getNameByOrderNumber(nextSpeakerId));
                nextSpeakerId++;
                if (nextSpeakerId > helper.getTeamgroup().size() - 1) {
                    textSpeaker.setText("׼ ��\r\n�� ��");
                    textSpeaker.setBackground(Color.RED);
                }
            }
        });

        JButton btnReset = new JButton("\u91CD\u7F6E");
        //����
        btnReset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textSpeaker.setText("");
                textSpeaker.setBackground(Color.YELLOW);
                nextSpeakerId = 1;
                helper.reset();
                tableModel = new TBTableModel(helper);
                table.setModel(tableModel);
                table.updateUI();
            }
        });
        GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.TRAILING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addContainerGap(142, Short.MAX_VALUE)
                                .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addComponent(btnshowName, GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                                                .addGap(35)
                                                .addComponent(textSpeaker, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)
                                                .addGap(29)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
                                                        .addComponent(btnReset, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                        .addComponent(btnSort, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                        .addComponent(btnFee, GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)))
                                        .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 457, GroupLayout.PREFERRED_SIZE))
                                .addGap(113))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(22)
                                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 248, GroupLayout.PREFERRED_SIZE)
                                .addGap(17)
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
                                        .addComponent(textSpeaker)
                                        .addComponent(btnshowName, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addComponent(btnFee)
                                                .addGap(18)
                                                .addComponent(btnSort)
                                                .addGap(18)
                                                .addComponent(btnReset)))
                                .addContainerGap(62, Short.MAX_VALUE))
        );

        tableModel = new TBTableModel(helper);
        table = new JTable(tableModel);
        TableColumnModel tableColumnModel = table.getColumnModel();
        tableColumnModel.getColumn(4).setCellEditor(new DefaultCellEditor(new JCheckBox()));
        scrollPane.setViewportView(table);
        frame.getContentPane().setLayout(groupLayout);
    }
}
